<!--
 *  Copyright (c) 2025 GraphQL Contributors
 *  All rights reserved.
 *
 *  This source code is licensed under the license found in the
 *  LICENSE file in the root directory of this source tree.
-->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>GraphiQL 5 with React 19 and GraphiQL Code Exporter</title>
    <style>
      body {
        margin: 0;
      }

      #graphiql {
        height: 100dvh;
      }

      .loading {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 4rem;
      }
    </style>
    <link rel="stylesheet" href="https://esm.sh/graphiql/dist/style.css" />
    <link
      rel="stylesheet"
      href="https://esm.sh/@graphiql/plugin-code-exporter/dist/style.css"
    />
    <!-- Note: the ?standalone flag bundles the module along with all of its `dependencies`, excluding `peerDependencies`, into a single JavaScript file. -->
    <script type="importmap">
      {
        "imports": {
          "react": "https://esm.sh/react@19.1.0",
          "react/jsx-runtime": "https://esm.sh/react@19.1.0/jsx-runtime",

          "react-dom": "https://esm.sh/react-dom@19.1.0",
          "react-dom/client": "https://esm.sh/react-dom@19.1.0/client",

          "graphiql": "https://esm.sh/graphiql?standalone&external=react,react-dom,@graphiql/react,graphql",
          "@graphiql/plugin-code-exporter": "https://esm.sh/@graphiql/plugin-code-exporter?standalone&external=react,@graphiql/react,graphql&deps=codemirror@5.65.3",
          "@graphiql/react": "https://esm.sh/@graphiql/react?standalone&external=react,react-dom,graphql",

          "@graphiql/toolkit": "https://esm.sh/@graphiql/toolkit?standalone&external=graphql",
          "graphql": "https://esm.sh/graphql@16.11.0",
          "regenerator-runtime/runtime": "https://esm.sh/regenerator-runtime/runtime"
        }
      }
    </script>
    <script type="module">
      // Import React and ReactDOM
      import React from 'react';
      import ReactDOM from 'react-dom/client';
      // Import GraphiQL and the Explorer plugin
      import { GraphiQL, HISTORY_PLUGIN } from 'graphiql';
      import { createGraphiQLFetcher } from '@graphiql/toolkit';
      // Required to be before `@graphiql/plugin-code-exporter`
      import 'regenerator-runtime/runtime';
      import { codeExporterPlugin } from '@graphiql/plugin-code-exporter';

      import createJSONWorker from 'https://esm.sh/monaco-editor/esm/vs/language/json/json.worker.js?worker';
      import createGraphQLWorker from 'https://esm.sh/monaco-graphql/esm/graphql.worker.js?worker';
      import createEditorWorker from 'https://esm.sh/monaco-editor/esm/vs/editor/editor.worker.js?worker';

      globalThis.MonacoEnvironment = {
        getWorker(_workerId, label) {
          console.info('MonacoEnvironment.getWorker', { label });
          switch (label) {
            case 'json':
              return createJSONWorker();
            case 'graphql':
              return createGraphQLWorker();
          }
          return createEditorWorker();
        },
      };

      const fetcher = createGraphiQLFetcher({
        url: 'https://countries.trevorblades.com',
      });
      function getQuery(arg, spaceCount = 2) {
        const spaces = ' '.repeat(spaceCount);
        const { query } = arg.operationDataList[0];
        return spaces + query.replaceAll('\n', '\n' + spaces);
      }

      const codeExporter = codeExporterPlugin({
        /**
         * Example code for snippets. See https://github.com/OneGraph/graphiql-code-exporter#snippets for details.
         */
        snippets: [
          {
            name: 'Example One',
            language: 'JavaScript',
            codeMirrorMode: 'jsx',
            options: [],
            generate: arg =>
              ['export const query = graphql`', getQuery(arg), '`'].join('\n'),
          },
          {
            name: 'Example Two',
            language: 'JavaScript',
            codeMirrorMode: 'jsx',
            options: [],
            generate: arg =>
              [
                "import { graphql } from 'graphql'",
                '',
                'export const query = graphql`',
                getQuery(arg),
                '`',
              ].join('\n'),
          },
        ],
      });

      const plugins = [HISTORY_PLUGIN, codeExporter];

      function App() {
        return React.createElement(GraphiQL, {
          fetcher,
          plugins,
          defaultEditorToolsVisibility: true,
        });
      }

      const container = document.getElementById('graphiql');
      const root = ReactDOM.createRoot(container);
      root.render(React.createElement(App));
    </script>
  </head>
  <body>
    <div id="graphiql">
      <div class="loading">Loading…</div>
    </div>
  </body>
</html>
